using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace jtifedit {
    public partial class ThumbCtl : UserControl {
        public ThumbCtl() {
            InitializeComponent();

            Resize += new EventHandler(ThumbCtl_Resize);
            PaddingChanged += new EventHandler(ThumbCtl_PaddingChanged);
        }

        int i = 0;

        public int PGIndex {
            get {
                return i;
            }
            set {
                i = value;
            }
        }

        bool copyOnWrite = false;

        public bool PGCopyOnWrite { get { return copyOnWrite; } set { copyOnWrite = value; } }

        public Bitmap PGImage {
            get {
                return pic;
            }
            set {
                pic = (Bitmap)value;
            }
        }

        Bitmap thumb = null;

        public Bitmap Thumbnail {
            get {
                return thumb;
            }
            set {
                thumb = value;

                Recalc();
            }
        }

        private void Recalc() {
            int cx = thumb != null ? thumb.Width : 32;
            int cy = thumb != null ? thumb.Height : 32;

            rcDrawPic = new Rectangle(Padding.Left, Padding.Top, cx, cy);
            rcText = new Rectangle(Padding.Left, rcDrawPic.Bottom, rcDrawPic.Width, 30 + Padding.Bottom);

            MinimumSize = new Size(Padding.Horizontal + cx, Padding.Vertical + 30 + cy);
            Invalidate();
        }

        Bitmap pic = null;
        Rectangle rcDrawPic, rcText;

        String text = "";

        public override String Text {
            get {
                return text;
            }
            set {
                text = value;
                Invalidate();
            }
        }

        bool isSel = false;

        public bool Selected {
            get {
                return isSel;
            }
            set {
                if (isSel != value) {
                    isSel = value;

                    BackColor = !isSel ? Parent.BackColor : Color.FromKnownColor(KnownColor.Highlight);
                    ForeColor = !isSel ? Parent.ForeColor : Color.FromKnownColor(KnownColor.HighlightText);

                    Invalidate();
                }
            }
        }

        private void ThumbCtl_Load(object sender, EventArgs e) {
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        }

        void ThumbCtl_PaddingChanged(object sender, EventArgs e) {
            Recalc();
        }

        void ThumbCtl_Resize(object sender, EventArgs e) {
            Recalc();
        }

        private void ThumbCtl_Paint(object sender, PaintEventArgs e) {
            if (thumb != null) {
                e.Graphics.DrawImage(thumb, rcDrawPic);
            }
            if (text != null) {
                StringFormat sf = new StringFormat();
                sf.LineAlignment = sf.Alignment = StringAlignment.Center;
                e.Graphics.DrawString(text, this.Font, new SolidBrush(ForeColor), rcText, sf);
            }
        }
    }
}
